package com.ruoyi.zhgd.controller;

import java.util.List;
import java.util.Arrays;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.zhgd.dto.WarnParamDto;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhgd.domain.vo.CollectDosageVo;
import com.ruoyi.zhgd.domain.bo.CollectDosageBo;
import com.ruoyi.zhgd.service.ICollectDosageService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消耗信息
 *
 * @author zyb
 * @date 2023-09-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhgd/dosage")
public class CollectDosageController extends BaseController {

    private final ICollectDosageService iCollectDosageService;

    /**
     * 查询消耗信息列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<CollectDosageVo> list(CollectDosageBo bo, PageQuery pageQuery) {
        return iCollectDosageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出消耗信息列表
     */
    @SaCheckLogin
    @Log(title = "消耗信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CollectDosageBo bo, HttpServletResponse response) {
        List<CollectDosageVo> list = iCollectDosageService.queryList(bo);
        ExcelUtil.exportExcel(list, "消耗信息", CollectDosageVo.class, response);
    }

    /**
     * 获取消耗信息详细信息
     *
     * @param pkId 主键
     */
    @SaCheckLogin
    @GetMapping("/{pkId}")
    public R<CollectDosageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long pkId) {
        return R.ok(iCollectDosageService.queryById(pkId));
    }

    /**
     * 新增消耗信息
     */
    @SaCheckLogin
    @Log(title = "消耗信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CollectDosageBo bo) {
        return toAjax(iCollectDosageService.insertByBo(bo));
    }

    /**
     * 修改消耗信息
     */
    @SaCheckLogin
    @Log(title = "消耗信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CollectDosageBo bo) {
        return toAjax(iCollectDosageService.updateByBo(bo));
    }

    /**
     * 删除消耗信息
     *
     * @param pkIds 主键串
     */
    @SaCheckLogin
    @Log(title = "消耗信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pkIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] pkIds) {
        return toAjax(iCollectDosageService.deleteWithValidByIds(Arrays.asList(pkIds), true));
    }

    /**
     * 实时预警
     * @param pkId
     * @return
     */
    @SaIgnore
    @GetMapping("/test/{pkId}")
    public R test(@PathVariable String pkId){
        return R.ok(iCollectDosageService.test(pkId));
    }


    /**
     * 修正
     * @param pkId
     * @return
     */
//    @SaIgnore
    @GetMapping("/handleCorrect/{pkId}")
    public R handleCorrect(@PathVariable String pkId){
        return iCollectDosageService.handleCorrect(pkId);
    }
    /**
     * 手动预警
     * @return
     */
    @GetMapping("/warning")
    public R warning(){
        return R.ok(iCollectDosageService.warning());
    }

    /**
     * 手动预警多条
     * @return
     */
    @PostMapping("/warningTest")
    public R warning(@RequestBody WarnParamDto dto) {
        List<String> ids = dto.getIds();
        if (ids == null || ids.size() == 0) {
            return R.fail("请勾选至少一条数据");
        }
        String msg = "";
        for (String id : ids) {
            msg += "pkId:" + id + ",";
            msg += iCollectDosageService.test(id);
        }
        return R.ok("操作成功", msg);
    }
}
